﻿#region Imports
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
#endregion

namespace LinqDemo.Converters
{
    public class VisibilityConverter : IValueConverter
    {
        #region Methods
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (IsBoolean(value))
            {
                if ((string)parameter == "-") value = !System.Convert.ToBoolean(value);
                return System.Convert.ToBoolean(value) ? Visibility.Visible : Visibility.Collapsed;
            }
            else return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private bool IsBoolean(object value)
        {
            try { System.Convert.ToBoolean(value); return true; }
            catch (Exception) { return false; }
        }
        #endregion
    }
}
